package aos.system.modules.people;

import java.util.List;

import aos.ydksxt.util.GuidUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import aos.framework.core.dao.SqlDao;
import aos.framework.core.typewrap.Dto;
import aos.framework.core.utils.AOSJson;
import aos.framework.core.utils.AOSUtils;
import aos.framework.web.router.HttpModel;
import aos.system.common.id.IdService;
import aos.system.common.utils.SystemCons;
import aos.system.dao.AosGroupDao;
import aos.system.dao.AosOrgDao;
import aos.system.dao.AosUserGroupDao;
import aos.system.dao.po.AosOrgPO;
import aos.system.dao.po.AosUserGroupPO;
import aos.system.modules.cache.CacheUserDataService;

/**
 * 群组管理
 * 
 * @author yushaoyang
 *
 */
@Service
public class PeopleService {
	
	@Autowired
	private AosGroupDao aosGroupDao;
	@Autowired
	private AosOrgDao aosOrgDao;
	@Autowired
	private SqlDao sqlDao;
	@Autowired
	private IdService idService;	
	@Autowired
	private CacheUserDataService cacheUserDataService;
	@Autowired
	private AosUserGroupDao aosUserGroupDao;
	/**
	 * 群组管理页面初始化
	 * 
	 * @param httpModel
	 * @return
	 */
	public void init(HttpModel httpModel) {
		AosOrgPO aosOrgPO = aosOrgDao.selectByKey(httpModel.getUserModel().getAosOrgPO().getId());
		httpModel.setAttribute("rootPO", aosOrgPO);
		httpModel.setAttribute("super_user_id", SystemCons.SUPER_USER_ID);
		httpModel.setViewPath("system/people.jsp");
	}


	/**
	 * 查询待选角色列表
	 * 
	 * @param httpModel
	 * @return
	 */
	public void listRoles(HttpModel httpModel) {
		AosOrgPO aosOrgPO = httpModel.getUserModel().getAosOrgPO();
		Dto qDto = httpModel.getInDto();
		qDto.put("org_cascade_id", aosOrgPO.getCascade_id());
		//qDto.put("is_del", SystemCons.IS.NO);
		List<Dto> roleDtos = sqlDao.list("People.listToSelectRoles", qDto);
		
		httpModel.setOutMsg(AOSJson.toGridJson(roleDtos, roleDtos.size()));
	}
	
	/**
	 * 查询已选角色列表
	 * 
	 * @param httpModel
	 * @return
	 */
	public void listSelectedRoles(HttpModel httpModel) {
		AosOrgPO aosOrgPO = httpModel.getUserModel().getAosOrgPO();
		Dto qDto = httpModel.getInDto();
		qDto.put("org_cascade_id", aosOrgPO.getCascade_id());
		List<Dto> roleDtos = sqlDao.list("People.listSelectedRoles", qDto);
		
		httpModel.setOutMsg(AOSJson.toGridJson(roleDtos, roleDtos.size()));
	}
	
	/**
	 * 保存用户角色关联关系
	 * 
	 * @param httpModel
	 */
	@Transactional
	public void saveUserRoleGrantInfo(HttpModel httpModel) {
		Dto inDto = httpModel.getInDto();
		String user_id = inDto.getString("user_id");
		String[] selectionIds = httpModel.getInDto().getRows();
		for (String id : selectionIds) {
			AosUserGroupPO aosUserGroupPO = new AosUserGroupPO();
			aosUserGroupPO.setId(GuidUtils.getRandomUUID());
			aosUserGroupPO.setGroup_id(String.valueOf(id));
			aosUserGroupPO.setUser_id(user_id);
			aosUserGroupPO.setCreate_by(httpModel.getUserModel().getId());
			aosUserGroupPO.setCreate_time(AOSUtils.getDateTime());
			aosUserGroupDao.insert(aosUserGroupPO);
		}
		cacheUserDataService.resetGrantInfoOfUser(user_id);
		httpModel.setOutMsg("用户群组关联关系保存成功。");
	}
	
	/**
	 * 撤消用户角色关联关系
	 * 
	 * @param httpModel
	 */
	@Transactional
	public void delUserRoleGrantInfo(HttpModel httpModel) {
		String[] selectionIds = httpModel.getInDto().getRows();
		for (String id : selectionIds) {
			AosUserGroupPO aosUserGroupPO = aosUserGroupDao.selectByKey(String.valueOf(id));
			aosUserGroupDao.deleteByKey(String.valueOf(id));
			cacheUserDataService.resetGrantInfoOfUser(aosUserGroupPO.getUser_id());
		}
		httpModel.setOutMsg("撤消用户群组关联关系成功。");
	}

	
}
